package com.bachongLightNovel.works.listener;

import com.alibaba.fastjson2.JSON;
import com.bachongLightNovel.constant.RabbitmqConstant;
import com.bachongLightNovel.model.pojo.works.Collect;
import com.bachongLightNovel.works.config.RabbitmqConfig;
import com.bachongLightNovel.works.mapper.WorksCollectMapper;
import com.bachongLightNovel.works.mapper.WorksNovelDetailMapper;
import com.bachongLightNovel.works.mapper.WorksNovelMapper;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * @author vermouth
 */

@Component
@Slf4j
public class RabbitmqListener {

    @Autowired
    private WorksNovelMapper worksNovelMapper;

    @Autowired
    private WorksNovelDetailMapper worksNovelDetailMapper;

    @Autowired
    private WorksCollectMapper worksCollectMapper;

    /**
     * @param novelId: 需要删除的小说id
     * @param channel:
     * @param tag:     当前消息的位置
     * @return void
     */
    @RabbitListener(
            bindings = {
                    @QueueBinding(value = @Queue(value = RabbitmqConfig.BACHONG_NOVEL_DELETE_QUEUE, durable = "true"),
                            exchange = @Exchange(value = RabbitmqConfig.BACHONG_NOVEL_EXCHANGE),
                            key = RabbitmqConfig.BACHONG_NOVEL_DELETE_KEY)
            }
    )
    @Transactional
    public void novelDeleteListener(Long novelId, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        try {
            log.info("接收到异步消息：{}", novelId);
            //删除小说章节信息
            worksNovelDetailMapper.deleteByNovelId(novelId);
            //删除小说信息
            worksNovelMapper.deleteById(novelId);
            log.info("异步删除小说相关信息执行成功。。。");
            channel.basicAck(tag, false);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    /**
     * 监听收藏消息队列
     * @param json:
     * @param channel:
     * @param tag:
     * @return void
     */
    @RabbitListener(queues = RabbitmqConstant.BACHONG_NOVEL_COLLECT_QUEUE)
    public void collectListener(String json, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        try {
            log.info("收藏队列接收到异步消息：{}", json);
            Collect collect = JSON.parseObject(json, Collect.class);
            if (collect.getIsCollect()) {
                //项数据库中插入一条数据
                worksCollectMapper.insert(collect);
            } else {
                //在数据库中，删除相应数据
                worksCollectMapper.deleteByQuery(collect);
            }
            channel.basicAck(tag, false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
